package com.open.capacity.salary.controller;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.open.capacity.commons.Result;
import com.open.capacity.salary.dto.RecordTaxExcelModel;
import com.open.capacity.salary.entity.GzRecordSalary;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.salary.entity.GzRecordTax;
import com.open.capacity.salary.service.GzRecordTaxService;

import com.open.capacity.commons.PageResult;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 工资系统扣税记录
 *
 * @author
 * @email
 * @date 2021-08-09 14:19:25
 */
@RestController
@RequestMapping("gzrecordtaxs")
@Api(tags = "工资系统扣税记录")
public class GzRecordTaxController {

    @Autowired
    private GzRecordTaxService gzRecordTaxService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('gz_record_tax:gzrecordtax:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = gzRecordTaxService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('gz_record_tax:gzrecordtax:list')")
    public Result findById(String id) {
        return Result.succeed(gzRecordTaxService.findById(id));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate1")
    @PreAuthorize("hasAnyAuthority('gz_record_tax:gzrecordtax:save')")
    public Result sasaveOrUpdate1ve(@RequestBody @Valid GzRecordTax gzRecordTax, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            gzRecordTaxService.save(gzRecordTax);
        } else {
            gzRecordTax.setId(keyValue);
            gzRecordTaxService.update(gzRecordTax);
        }
        return Result.succeed("保存成功");
    }


    /**
     * 批量保存
     */
    @RequestMapping("/saveOrUpdate2")
    @PreAuthorize("hasAnyAuthority('gz_record_tax:gzrecordtax:save')")
    public Result saveOrUpdate2(String companyId, String ztId, @RequestBody @Valid List<GzRecordTax> list) {
        gzRecordTaxService.batchSave(companyId, ztId, list);
        return Result.succeed("保存成功");
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('gz_record_tax:gzrecordtax:delete')")
    public Result delete(@PathVariable String id) {
        gzRecordTaxService.delete(id);
        return Result.succeed("删除成功");
    }

    /**
     * 导入excel
     */
    @PostMapping("/importExcel")
    @PreAuthorize("hasAnyAuthority('gz_record_tax:gzrecordtax:import')")
    public Result importExcel(String companyId, String ztId, @RequestParam("file") MultipartFile multipartFile) throws Exception {
        int count = gzRecordTaxService.importExcel(companyId, ztId, multipartFile);
        return Result.succeed(count, "导入成功");
    }

    /**
     * 导出数据
     */
    @GetMapping("/exportExcel")
    @PreAuthorize("hasAnyAuthority('gz_record_tax:gzrecordtax:export')")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        List<GzRecordTax> list = gzRecordTaxService.findAll(params);

        List<RecordTaxExcelModel> result = list.stream().map(item -> {
            RecordTaxExcelModel model = new RecordTaxExcelModel();
            BeanUtils.copyProperties(item, model);
            return model;
        }).collect(Collectors.toList());

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=recordTax.xls");
        OutputStream ouputStream = null;
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(),
                RecordTaxExcelModel.class, result);
        try {
            ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
        } catch (Exception e) {
            throw new RuntimeException("系统异常");
        } finally {
            try {
                if (ouputStream != null) {
                    ouputStream.flush();
                    ouputStream.close();
                }
            } catch (Exception e) {
                throw new RuntimeException("系统异常");
            }
        }
    }

}
